<?php
class Manager_PreferenceGroup
{
	/**
	 * Group saying that user has no specified preferences
	 * @var string
	 */
	const GROUP_DEFAULT = 'default';
	
	public static function initialize()
	{
		$names = array(self::GROUP_DEFAULT);
		
		$criteria = new Criteria();
		
		try {
			foreach ($names as $name) {
				$criteria->clear();
				$criteria->add(PreferencesgroupsPeer::NAME, $name, Criteria::EQUAL);
				
				if (CategoriesPeer::doCount($criteria) === 0) {
					// add to database missing reqest statuses
					$category = new Preferencesgroups();
					$category->setName($name);
					$category->setCategory1(Manager_Category::getDefault());
					$category->setCategory2(Manager_Category::getDefault());
					$category->setCategory3(Manager_Category::getDefault());
					$category->setCategory4(Manager_Category::getDefault());
					$category->setCategory5(Manager_Category::getDefault());
					$category->save();
				}
			}
		} catch (Exception $e) {
			throw new Exception('Preferences groups could not be initialized in the DB. ' . $e->getMessage());
		}
	}
	
	/**
	 * 
	 * @return Preferencesgroups
	 */
	public static function getDefault()
	{
		$criteria = new Criteria();
		$criteria->add(
			PreferencesgroupsPeer::NAME, 
			self::GROUP_DEFAULT, 
			Criteria::EQUAL
		);
		
		$group = PreferencesgroupsPeer::doSelectOne($criteria);
		
		if (empty($group)) {
			throw new Exception('Default preference group does not exist.');
		}
		
		return $group;
	}
}